package org.jeecg.modules.product.webController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.company.entity.JkCompany;
import org.jeecg.modules.company.service.IJkCompanyService;
import org.jeecg.modules.product.entity.JkProduct;
import org.jeecg.modules.product.service.IJkProductService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.util.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Slf4j
@Tag(name = "前端product产品接口")
@RestController
@RequestMapping("/api/product")
public class JKProductWebController {

    @Autowired
    private IJkProductService jkProductService;

    @Autowired
    private IJkCompanyService jkCompanyService;

    @Autowired
    DictUtils dictUtils;

    @Operation(summary="web_jk_product-分页列表查询")
    @GetMapping(value = "/list")
    @IgnoreAuth
    public Result<IPage<JkProduct>> queryPageList(JkProduct jkProduct,
                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<JkProduct> queryWrapper = QueryGenerator.initQueryWrapper(jkProduct, req.getParameterMap());
        queryWrapper.orderByAsc("sn");
        Page<JkProduct> page = new Page<JkProduct>(pageNo, pageSize);
        IPage<JkProduct> pageList = jkProductService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @Operation(summary="jk_product-通过id查询")
    @GetMapping(value = "/queryById")
    @IgnoreAuth
    public Result<JkProduct> queryById(@RequestParam(name="id",required=true) String id,  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) throws InvocationTargetException, IllegalAccessException {
        JkProduct jkProduct = jkProductService.getById(id);

        if(jkProduct==null) {
            return Result.error("未找到对应数据");
        }
        String productType = dictUtils.getDictItemName("product_type", jkProduct.getProducttype());
        jkProduct.setProducttype(productType);

        String productStatus = dictUtils.getDictItemName("product_status",jkProduct.getIsavailable());
        jkProduct.setProducttype(productStatus);
        JkCompany jkCompany = jkCompanyService.getById(jkProduct.getCompanyid());
        jkProduct.setCompanyid(jkCompany.getName());
        return Result.OK(jkProduct);
    }


    @Operation(summary="jk_product-通过公司查询")
    @GetMapping(value = "/queryByCompanyId")
    @IgnoreAuth
    public Result<IPage<JkProduct>> queryByCompanyId(@RequestParam(name="companyId",required=true) String companyId,  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

   /*     List<JkProduct> list = jkProductService.queryByCompanyId(companyId);
        return Result.OK(list);*/
           JkProduct jkProduct = new JkProduct();
        jkProduct.setCompanyid(companyId);
        QueryWrapper<JkProduct> queryWrapper = QueryGenerator.initQueryWrapper(jkProduct, null);
            Page<JkProduct> page = new Page<JkProduct>(pageNo, pageSize);
        IPage<JkProduct> pageList = jkProductService.page(page, queryWrapper);

        return Result.OK(pageList);
    }


}
